package com.bsoft.gol.editor.service.impl;

import com.bsoft.gol.editor.dao.EditorUserDAO;
import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.EditorUser;
import com.bsoft.gol.editor.editor.qo.EditorUserQo;
import com.bsoft.gol.editor.editor.response.QueryEditorUserListResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bsoft.gol.editor.service.EditorUserApi;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service("editorUserApi")
@Transactional
public class EditorUserApiImpl implements EditorUserApi {

    @Autowired
    EditorUserDAO editorUserDAO;


    @Override
    public BaseResponse<EditorUser> login(EditorUser req)  {
        BaseResponse<EditorUser> baseResponse = new BaseResponse();
        req.setLoginName(req.getUserName());
        if(CommonUtils.isNotNull(req.getLoginUserId())){
            EditorUser editorUser=editorUserDAO.get(req.getLoginUserId());
            if(CommonUtils.isNull(editorUser)){
                req.setStatus("0");
            req.setCreateTime(new Date());
            req.setLastTime(new Date());
            editorUserDAO.save(req);
            }else if("1".equals(req.getStatus())){
                baseResponse.setCode(500);
                baseResponse.setMessage("用户已被禁用，登陆失败");
                return baseResponse;
            }else if(!"2".equals(req.getUserType())){
                if(CommonUtils.isNotNull(editorUser.getPassWord())){
                    req.setPassWord(editorUser.getPassWord());
                }
                req.setStatus("0");
                req.setLastTime(new Date());
                editorUserDAO.update(req);
            }
            baseResponse.setData(req);
            baseResponse.setMessage("登陆成功");
        }else {

            EditorUser editorUser = editorUserDAO.getEditorUserByLoginName(req.getLoginName());
            if (editorUser == null) {
                baseResponse.setCode(500);
                baseResponse.setMessage("该用户未注册");
            } else if("1".equals(editorUser.getStatus())){
                baseResponse.setCode(500);
                baseResponse.setMessage("用户已被禁用，登陆失败");
            } else if (req.getPassWord().equals(editorUser.getPassWord())||(CommonUtils.isNull(editorUser.getPassWord())&& "123456".equals(req.getPassWord()))) {
                baseResponse.setData(editorUser);
                baseResponse.setMessage("登陆成功");
            } else {
                baseResponse.setCode(500);
                baseResponse.setMessage("密码错误，登陆失败");
            }
        }
        return baseResponse;
    }



    @Override
    public BaseResponse<EditorUser> register(EditorUser req)  {
        BaseResponse<EditorUser> baseResponse = new BaseResponse();
        if(CommonUtils.isNotNull(req.getLoginUserId())){
            EditorUser editorUser=editorUserDAO.get(req.getLoginUserId());
            if(CommonUtils.isNull(editorUser)){
                baseResponse.setCode(500);
                baseResponse.setMessage("该用户不存在");
                return baseResponse;
            }else{
                req.setUpdateTime(new Date());
                editorUserDAO.update(req);
            }
        }else {
            EditorUser editorUser=editorUserDAO.getEditorUserByOgrId(req.getLoginName());

            if(CommonUtils.isNotNull(editorUser)){
            baseResponse.setCode(500);
            baseResponse.setMessage("该用户名已被使用");
             }else {
            req.setLoginUserId(UUID.randomUUID().toString());
            req.setCreateTime(new Date());
            editorUserDAO.save(req);
            baseResponse.setData(req);
            baseResponse.setMessage("注册成功");
        }
        }
        return baseResponse;
    }


    @Override
    public BaseResponse<QueryEditorUserListResponse> list(EditorUserQo req) {
        QueryEditorUserListResponse queryEditorUserListResponse = new QueryEditorUserListResponse();

        List<EditorUser> editorUserList = editorUserDAO.queryEditorUserList(req);
        queryEditorUserListResponse.setList(editorUserList);
        queryEditorUserListResponse.setTotal(editorUserDAO.queryUserCount(req));
        BaseResponse<QueryEditorUserListResponse> baseResponse = new BaseResponse<>();
        baseResponse.setData(queryEditorUserListResponse);

        return baseResponse;
    }



    @Override
    public BaseResponse<EditorUser> delete(EditorUser req)  {
        BaseResponse<EditorUser> baseResponse = new BaseResponse();
        EditorUser editorUser=editorUserDAO.get(req.getLoginUserId());
        if(CommonUtils.isNotNull(editorUser)){
            editorUserDAO.remove(req.getLoginUserId());
        }else{

                baseResponse.setCode(500);
                baseResponse.setMessage("该用户不存在");
        }
        return baseResponse;
    }



    @Override
    public BaseResponse<QueryEditorUserListResponse> deptList(EditorUserQo req) {
        QueryEditorUserListResponse queryEditorUserListResponse = new QueryEditorUserListResponse();

        List<EditorUser> editorUserList = editorUserDAO.queryEditorDeptList(req);
        queryEditorUserListResponse.setList(editorUserList);
        queryEditorUserListResponse.setTotal(editorUserDAO.queryDeptCount(req));
        BaseResponse<QueryEditorUserListResponse> baseResponse = new BaseResponse<>();
        baseResponse.setData(queryEditorUserListResponse);

        return baseResponse;
    }

}
